export function initAccessorySpecs(accessorySpecs = [], colorTableData = [], sizeTableData = []) {
    // 获取唯一尺码 颜色
    const uniqueSizeColorObj = {};
    accessorySpecs.forEach((m) => {
        if (m.accessoryType == "斋架" || m.accessoryType == "齋架") {
            const {
                sizeList = []
            } = m;
            // 颜色
            sizeList.forEach((size, index) => {
                if (colorTableData.length > 0) {
                    colorTableData.forEach((color, index2) => {
                        const key = size.sizeNum + " " + color.colorNum;
                        if (!uniqueSizeColorObj[key]) {
                            uniqueSizeColorObj[key] = {};
                            uniqueSizeColorObj[key].children = [];
                        }
                        if (!uniqueSizeColorObj[key].children[0]) {
                            uniqueSizeColorObj[key].children[0] = {
                                ID: "ZJ" + index + "_" + index2,
                                accessoryName: m.accessoryType,
                                children: [],
                            };
                        }
                        uniqueSizeColorObj[key].children[0].children.push(m);
                    });
                }
            });
            // 吉架
            const sList = sizeTableData
            sList.forEach((size, index) => {
                if (size.luckTilt && sizeList.some(s => s.sizeNum === size.sizeNum)) {
                    const key = size.sizeNum + " 吉架";
                    if (!uniqueSizeColorObj[key]) {
                        uniqueSizeColorObj[key] = {};
                        uniqueSizeColorObj[key].children = [];
                    }
                    if (m.accessoryType === "斋架" || m.accessoryType === "齋架") {
                        if (!uniqueSizeColorObj[key].children[0]) {
                            uniqueSizeColorObj[key].children[0] = {
                                ID: "ZJ" + index,
                                accessoryName: m.accessoryType,
                                children: [],
                            };
                        }
                        uniqueSizeColorObj[key].children[0].children.push(m);
                    }
                }
            });
        }
        if (m.accessoryType == "配件") {
            const {
                sizeList = [], colorList = []
            } = m;
            // 颜色
            sizeList.forEach((size, index) => {
                if (colorList.length > 0) {
                    colorList.forEach((color, index2) => {
                        const key = size.sizeNum + " " + color;
                        if (!uniqueSizeColorObj[key]) {
                            uniqueSizeColorObj[key] = {};
                            uniqueSizeColorObj[key].children = [];
                        }
                        if (!uniqueSizeColorObj[key].children[1]) {
                            uniqueSizeColorObj[key].children[1] = {
                                ID: "PJ" + index + "_" + index2,
                                accessoryName: m.accessoryType,
                                children: [],
                            };
                        }
                        uniqueSizeColorObj[key].children[1].children.push(m);
                    });
                } else {
                    // 这里判断颜色列表是否为空 但是 真实数据不会出现这种问题
                    // 获取空值的颜色
                    const crs = colorTableData
                    crs.map(cm => {
                        const key = size.sizeNum + ' ' + cm.colorNum
                        if (!uniqueSizeColorObj[key]) {
                            uniqueSizeColorObj[key] = {};
                            uniqueSizeColorObj[key].children = [];
                        }
                        if (m.accessoryType === "斋架" || m.accessoryType === "齋架") {
                            if (!uniqueSizeColorObj[key].children[0]) {
                                uniqueSizeColorObj[key].children[0] = {
                                    ID: "ZJ" + index,
                                    accessoryName: m.accessoryType,
                                    children: [],
                                };
                            }
                            uniqueSizeColorObj[key].children[0].children.push(m);
                        } else if (m.accessoryType === "配件") {
                            if (!uniqueSizeColorObj[key].children[1]) {
                                uniqueSizeColorObj[key].children[1] = {
                                    ID: "PJ" + index,
                                    accessoryName: "配件",
                                    children: [],
                                };
                            }
                            uniqueSizeColorObj[key].children[1].children.push(m);
                        }
                    })

                }
            });
        }
    })
    console.log("uniqueSizeColorObj=====================", uniqueSizeColorObj);
    // 获取对应尺码对应的颜色MAP
    const std = sizeTableData
    const stdMap = new Map()
    if (std.length > 0) {
        // 按顺序组装颜色和吉架 2个map是为了保证顺序
        std.map(m => {
            if (m.colorNum) {
                for (let key in m.colorNum) {
                    if (!stdMap.has(m.sizeNum + ' ' + key)) {
                        stdMap.set(m.sizeNum + ' ' + key, m.colorNum[key])
                    }
                }
            }
        })
        std.map(m => {
            if (m.luckTilt) {
                if (!stdMap.has(m.sizeNum + ' 吉架')) {
                    stdMap.set(m.sizeNum + ' 吉架', m.luckTilt)
                }
            }
        })

    }
    console.log("stdMap=====================", stdMap);
    // 将对象转换成数组
    const uniqueSizeColors = [];
    stdMap.forEach((value, key) => {
        if (value && uniqueSizeColorObj[key] && (uniqueSizeColorObj[key]?.children && uniqueSizeColorObj[key]?.children.length > 0)) {
            let keyText = ''
            if (key.indexOf('吉架') === -1) {
                keyText = key + '色 ' + "(" + value + ")付"
            } else {
                keyText = key + " (" + value + ")付"
            }
            uniqueSizeColors.push({
                accessoryName: keyText,
                children: uniqueSizeColorObj[key]?.children || [],
            });
        }
    })
    console.log("uniqueSizeColors=====================", uniqueSizeColors);
    // 增加层级数
    const fn = (data, index = 1, seen = new WeakMap()) => {
        return data.map((m) => {
            if (seen.has(m)) {
                return m;
            }
            seen.set(m, true);

            if (index > 1) {
                m.accessoryName = `${index - 1} ${m.accessoryName}`;
            }

            if (m.children) {
                m.children = fn(m.children, index + 1, seen);
            }

            return m;
        });
    };
    fn(uniqueSizeColors, 0)
    return uniqueSizeColors;
}